/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import mx.containers.Canvas;
	/**
	 * 
	 * EDIApplication中控制显示EDICanvas的组件，非可视控件
	 * <p>在as中用法：</p>
	 * <p>pageController=new EDIPageController(ownerContainer);//ownerContainer是在EDIApplication中用于显示EDICanvas的容器</p>
	 * <p>pageController.showPageByName("MailNew");//根据传入的EDICanvas名称切换到相应的页面</p>
	 * 
	 * 
	 */	
	public class EDIPageController
	{
		private var pageList:Array;
		
		private var parentContainer:Canvas;
		
		private var activedPage:Object;  //当前活动页面，object = PageWrapper {name:String,page:EDIPage}

		public var lastPageIndex:int=-1;
		
		public function EDIPageController(pageContainer:Canvas){
			this.parentContainer = pageContainer;
			this.pageList = new Array();
		}
		/**
		 * 添加一个内置页面 
		 */
		public function addPage(page:EDICanvas):void{
			var pageWrapper:Object = new Object();
			//trace(page.name); 
			page.setHtmlPlanVisible(false);
			pageWrapper["name"] = page.className;
			pageWrapper["page"] = page;
//			trace(pageList.indexOf(pageWrapper));
			if (parentContainer.getChildByName(page.name) == null){
				page.visible = false;
				page.pageController = this;
				page.percentWidth = 100;
				page.percentHeight = 100;
				pageList.push(pageWrapper);
				parentContainer.addChild(page);
			} 
			
//			var contentXml:XML=new XML(<content></content>);
//			var childXml:XML;
//			var propertyName:String;
//			var propertyValue:String;
//			var propertiesList:XMLList;
//			var instanceInfo:XML=describeType(page);//通过反射机制取出当前MXML中的信息
//			var htmlEditoName:String;
//			//trace(instanceInfo.toXMLString());    
//			//propertiesList=instanceInfo.accessor;    
//			trace(instanceInfo..accessor.(@type=="com.edi.runtime.component::EDIHTMLEdit"));
//			propertiesList= instanceInfo..accessor.(@type=="com.edi.runtime.component::EDIHTMLEdit"); 
//			for each(var propertyInfo:XML in propertiesList)
//			{
//				htmlEditoName=propertyInfo.@name;
//				if(page[htmlEditoName] is EDIHTMLEdit)
//					page.HtmlEditorArr.addItem(page[htmlEditoName]);
//			}
		}
		
		/**
		 * 删除一个内置页面
		 */
		public function movePage(page:EDICanvas):void{
			var index:int;
			if (parentContainer.getChildByName(page.name) != null){
				for(index=0;index<pageList.length;index++){
					var pageWrapper:Object = pageList[index];
					if (pageWrapper["page"] == page){
						if (activedPage == page){
							showPageByIndex(0);
						}
						parentContainer.removeChild(page);
						break;
					}
				}
			}
			index = pageList.indexOf(pageWrapper);
			if (index!=-1){
				pageList.splice(index,1);
			}
			parentContainer.removeChild(page);
		}

		/**
		 * 向下一个页面移动
		 */
		public function next():void{
			if (activedPage == null){
				activedPage = pageList[0];
				(EDICanvas(activedPage["page"])).actived();
			}else{
				var index:int = pageList.indexOf(activedPage);
				if (index != -1){
					var pageWrapper:Object;
					if (index < pageList.length-1){
						pageWrapper = pageList[index+1];
						(EDICanvas(pageWrapper["page"])).actived();
						(EDICanvas(activedPage["page"])).unActived();
						activedPage = pageWrapper;
					}
				}
			}
		}
		
		/**
		 * 向上一个页面移动
		 */
		public function previous():void{
			if (activedPage == null)
				return;
			var index:int = pageList.indexOf(activedPage);
			if (index >0){
				var pageWrapper:Object = pageWrapper = pageList[index-1];
				(EDICanvas(pageWrapper["page"])).actived();
				(EDICanvas(activedPage["page"])).unActived();
				activedPage = pageWrapper;
			}
		}

		/**
		 * 通过ID显示某个页面
		 */
		public function showPageByName(name:String):void{
			for(var index:int=0;index<pageList.length;index++){
				var pageWrapper:Object = pageList[index];
				//trace("pageWrapper:"+pageWrapper["page"].className);
				if (pageWrapper["name"] == name){
					(EDICanvas(pageWrapper["page"])).actived();
					if (activedPage!=null && activedPage!=pageWrapper){
						(EDICanvas(activedPage["page"])).unActived();
					}
					activedPage = pageWrapper;
					break;
				}
			}
		}
		
		/**
		 * 通过index显示某个页面
		 */
		public function showPageByIndex(index:int):void{
			var pageWrapper:Object = pageList[index];
			if (pageWrapper != null){
				(EDICanvas(pageWrapper["page"])).actived();
					if (activedPage!==null){
						(EDICanvas(activedPage["page"])).unActived();
					}
					activedPage = pageWrapper;
			}
		}

		/**
		 * 通过页面实例显示某个页面
		 * @param page
		 * 
		 */
		
		public function showPageByPage(page:EDICanvas):void
		{
			for(var index:int=0;index<pageList.length;index++){
				var pageWrapper:Object = pageList[index];
				if (pageWrapper["page"] == page){
					(EDICanvas(pageWrapper["page"])).actived();
					if (activedPage!==null){
						(EDICanvas(activedPage["page"])).unActived();
					}
					activedPage = pageWrapper;
					break;
				}
			}
		}
		/**
		 * 取得当前活动的页面实例
		 * @return 
		 * 
		 */		
		public function get activePage():EDICanvas
		{
			return activedPage["page"] as EDICanvas;	
		}
		/**
		 * 通过页面ID取得页面实例
		 * @param value
		 * @return 
		 * 
		 */		
		public function getPageByIndex(value:int):EDICanvas
		{
			var pageWrapper:Object = pageList[value];
			if (pageWrapper != null){
				return pageWrapper["page"] as EDICanvas
			}
			else
			{
				return null;
			}
		}
		/**
		 * 通过页面名称取得页面实例
		 * @param name
		 * @return 
		 * 
		 */		
		public function getPageByName(name:String):EDICanvas
		{
			for(var index:int=0;index<pageList.length;index++){
				var pageWrapper:Object = pageList[index];
				if (pageWrapper["name"].toString().toUpperCase() == name.toUpperCase()){
					return EDICanvas(pageWrapper["page"]);
				}
			}
			return null;
		}
		/**
		 * 取得当前活动的页面序号
		 * @return 
		 * 
		 */		
		public function get index():int
		{
			return pageList.indexOf(activedPage);
		}

	}
}